﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using VNet.VisiSuite.Service.Proxy;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Contact.Message;

namespace VNet.VisiSuite.VisiAdmin.Providers
{
    public class PlantProvider
    {
        public static void GetPlants(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                    };
                    e.Result = client.GetPlants(request);
                }
            }
        }

        public static void UpdatePlants(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var list = e.Argument as List<object>;
                    var client = new VNetVisiSuiteContractClient();
                    var request = new SavePlantRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Plants = list[0] as List<PlantItem>,
                        Areas = list[1] as List<AreaItem>,
                        Cells = list[2] as List<CellItem>,
                    };
                    e.Result = client.UpdatePlants(request);
                }
            }
        }

        public static void InsertPlant(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new SaveDataRequest<PlantItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = e.Argument as PlantItem,
                    };
                    e.Result = client.InsertPlant(request);
                }
            }
        }

        public static void UpdatePlant(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new SaveDataRequest<PlantItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = e.Argument as PlantItem,
                    };
                    e.Result = client.UpdatePlant(request);
                }
            }
        }

        public static void DeletePlant(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                if (worker != null && worker.CancellationPending == false)
                {
                    var client = new VNetVisiSuiteContractClient();
                    var request = new DeleteDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        DataId = (int)e.Argument,
                    };
                    e.Result = client.DeletePlant(request);
                }
            }
        }
    }
}
